﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    public class ClientConsumptionController : Controller
    {
        private readonly IProductCategoryRepository productCategoryRepository;
        private readonly IProductRepository productRepository;
        private readonly IClientConsumptionRepository clientConsumptionRepository;
        private readonly IRoomRepository roomRepository;
        private readonly IUnitOfWork unitOfWork;

        public ClientConsumptionController(IProductRepository productRepository, IProductCategoryRepository productCategoryRepository, IClientConsumptionRepository clientConsumptionRepository,IRoomRepository roomRepository, IUnitOfWork unitOfWork)
        {
            this.productRepository = productRepository;
            this.productCategoryRepository = productCategoryRepository;
            this.clientConsumptionRepository = clientConsumptionRepository;
            this.roomRepository = roomRepository;
            this.unitOfWork = unitOfWork;
        }
        public IList<ProductCategoryInfoModel> GetProductCatagory()
        {
            var productCategory = productCategoryRepository.GetAll().AsQueryable();
            IList<ProductCategoryInfoModel> productCategoryInfoModel = (from category in productCategory
                                                                        select new ProductCategoryInfoModel
                                                                                   {
                                                                                       ID = category.ID,
                                                                                       Name = category.Name,
                                                                                       Remark = category.Remark
                                                                                   }).ToList();

            return productCategoryInfoModel;
        }
        public IList<ProductInfoModel> GetProductByCatagory(int categoryId)
        {
            var products = productRepository.GetMany(x => x.ProductCategory.ID == categoryId);
            IList<ProductInfoModel> producInfoModel = (from product in products
                                                       select new ProductInfoModel
                                                       {
                                                           ID = product.ID,
                                                           Name = product.Name,
                                                           PriceOut = product.PriceOut,
                                                           Remark = product.Remark,
                                                           Count = product.Count
                                                       }).ToList();
            return producInfoModel;
        }




        public ActionResult SelectProduct()
        {
            if (Request["roomId"] != null)
            {
                Session["roomId"] = Request["roomId"];
            }
            return View(GetProductCatagory());
        }
        [GridAction]
        public ActionResult ProductCatagoryDetailsAjax()
        {
            return View(new GridModel(GetProductCatagory()));
        }
        [GridAction]
        public ActionResult ProdcutForCatagoryDetailsAjax(int Id)
        {
            return View(new GridModel(GetProductByCatagory(Id)));
        }
        /***************************************************客户消费信息管理**************************************************/
        public List<RoomInfoModel> GetAllRoomByConsumptionInfo()
        {
            var rooms = roomRepository.GetAll().AsQueryable();
            List<RoomInfoModel> lists = (from room in rooms
                                         from c in clientConsumptionRepository.GetAll().AsQueryable()
                                         where room.RoomState.ID.Equals(2)
                                         where c.Room.ID.Equals(room.ID)
                                         select new RoomInfoModel
                                                      {
                                                          ID = room.ID,
                                                          Name = room.Name,
                                                          BedNum = room.BedNum,
                                                          FloorName = room.Floor.Name,
                                                          RoomStateName = room.RoomState.Name,
                                                          RoomTypeName = room.RoomType.Name

                                                      }).Distinct(new ComparerRoom()).ToList();
            return lists;
        }
        public List<ClientConsumptionInfoModel> GetAllClientConsumptionInfo(string roomId)
        {
            var clientConsumptions = clientConsumptionRepository.GetAll().AsQueryable();
            List<ClientConsumptionInfoModel> lists = (from clientConsumption in clientConsumptions
                                                      where clientConsumption.Room.ID.Equals(roomId)
                                                      select new ClientConsumptionInfoModel
                                                       {
                                                           ID = clientConsumption.ID,
                                                           RoomId = clientConsumption.Room.ID,
                                                           ProductID = clientConsumption.Product.ID,
                                                           ProductName = clientConsumption.Product.Name,
                                                           PriceOut =clientConsumption.Product.PriceOut, 
                                                           ProductCount = clientConsumption.ProductCount,
                                                           ProductCost = clientConsumption.ProductCost,
                                                           ConsumeDate = clientConsumption.ConsumeDate,
                                                           SumAccount =clientConsumption.SumAccount,

                                                       }).ToList();
            return lists;
        }

        public ActionResult ClientConsumptionManage()
        {
            ViewData["room"] = GetAllRoomByConsumptionInfo();
            ViewData["roomId"] = GetAllRoomByConsumptionInfo().FirstOrDefault().ID;
            ViewData["sumAccount"] = GetAllClientConsumptionInfo(ViewData["roomId"].ToString()).FirstOrDefault().SumAccount;
            ViewData["clientConsumption"] = GetAllClientConsumptionInfo(ViewData["roomId"].ToString());
            Session["roomId"] = ViewData["roomId"];
            
            return View();
        }

        [GridAction]
        public ActionResult ShowAllRoom()
        {
            return View(new GridModel(GetAllRoomByConsumptionInfo()));
        }


        [GridAction]
        public ActionResult ShowAllClientConsumptionInfoAjax(string roomId)
        {

            return View(new GridModel(GetAllClientConsumptionInfo(roomId)));
        }
        [HttpPost]
        public string EditProductCount(string productCount)
        {
            Session["productCount"] = productCount;
            return productCount;
        }
        [GridAction]
        public ActionResult DeleteClientConsumptionInfo(string Id)
        {
            var consumption = clientConsumptionRepository.Get(x => x.ID == Id);
            /*****************************更新Product数据*****************************/
            var product = productRepository.Get(x => x.ID == consumption.Product.ID);
            if (product.Count != 0)
            {
                product.Count = product.Count + Convert.ToInt32(Session["productCount"].ToString()) - consumption.ProductCount;
                productRepository.Update(product);
                unitOfWork.Commit();
            }
            Session.Remove("productCount");
            /************************************************************************/
            consumption.SumAccount = consumption.SumAccount - consumption.ProductCost;
            var clientConsumptions = clientConsumptionRepository.GetMany(x => x.Room.ID == consumption.Room.ID);
            foreach (var clientConsumption in clientConsumptions)
            {
                clientConsumption.SumAccount = consumption.SumAccount;
                unitOfWork.Commit();
            }
            clientConsumptionRepository.Delete(consumption);
            unitOfWork.Commit();
            return View(new GridModel(GetAllClientConsumptionInfo(Session["roomId"].ToString())));
        }
        [GridAction]
        public ActionResult UpdateClientConsumptionInfo(string Id,int ProductID,string RoomId,int ProductCount)
        {
            var consumption = clientConsumptionRepository.Get(x => x.ID == Id);
            /*****************************更新Product数据*****************************/
            var product = productRepository.Get(x => x.ID == ProductID);
            if (product.Count != 0)
            {
                product.Count = product.Count + ProductCount - consumption.ProductCount;
                productRepository.Update(product);
                unitOfWork.Commit();
            }
            /************************************************************************/


            /*****************更新消费信息，使消费总价格SumAccount保持一致************/
            consumption.SumAccount = consumption.SumAccount - (consumption.ProductCost - ProductCount * consumption.Product.PriceOut);
            consumption.ProductCount = ProductCount;
            consumption.ProductCost = ProductCount*consumption.Product.PriceOut;
            var clientConsumptions = clientConsumptionRepository.GetMany(x => x.Room.ID == RoomId);
            foreach (var clientConsumption in clientConsumptions)
            {
                clientConsumption.SumAccount = consumption.SumAccount;
                unitOfWork.Commit();
            }
            clientConsumptionRepository.Update(consumption);
            unitOfWork.Commit();
            /************************************************************************/

            
            return View(new GridModel(GetAllClientConsumptionInfo(Session["roomId"].ToString())));
        }
        /*********************************************************************************************************************/


    }
}
public class ComparerRoom : IEqualityComparer<RoomInfoModel>
{
    public bool Equals(RoomInfoModel a, RoomInfoModel b)
    {
        return a.ID == b.ID;
    }

    public int GetHashCode(RoomInfoModel obj)
    {
        return 0;
    }
}